package com.vclee.fast.integration.power.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vclee.fast.integration.power.mapper.SysRoleMapper;
import com.vclee.fast.integration.power.mapper.SysUserRoleMapper;
import com.vclee.fast.integration.power.model.SysRole;
import com.vclee.fast.integration.power.model.SysRoleMenu;
import com.vclee.fast.integration.power.model.SysUserRole;
import com.vclee.fast.integration.power.service.SysRoleService;
import com.vclee.fast.utils.exception.Assert;
import com.vclee.fast.utils.model.Bson;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class SysRoleServiceImpl implements SysRoleService {

    private final SysRoleMapper roleMapper;
    private final SysUserRoleMapper userRoleMapper;


    @Override
    public void addRole(SysRole sysRole) {
        roleMapper.insert(sysRole);
    }

    @Override
    public void editRole(SysRole sysRole) {
        Assert.claim(sysRole.getId() == null, "操作失败，主键为空");
        roleMapper.updateById(sysRole);
    }

    @Override
    public void delRole(SysRole sysRole) {
        Assert.claim(sysRole.getId()==null,"操作失败，主键为空");
        LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserRole::getRoleId,sysRole.getId());
        Assert.claim(userRoleMapper.selectCount(wrapper)>0,"存在用户使用了该角色,请先解除角色绑定");
        roleMapper.deleteById(sysRole.getId());
    }

    @Override
    public IPage<SysRole> findListByQuery(Bson query) {
//        int allCount = SysCustomMapper.countSysRole();
//        List<SysRole> list = new ArrayList<>();
//        if (allCount > 0) {
//            list = SysCustomMapper.findSysRoleList();
//        }
//        return new PageResult<>(allCount, queryParams.getSize(), queryParams.getPage(), list);
        Integer crt = query.getInteger("crt");
        Integer size = query.getInteger("size");
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        String name = query.getString("name");
        wrapper.like(!Assert.isBlank(name),SysRole::getRoleName,name);
        Integer orgId = query.getInteger("orgId");
        wrapper.eq(!Assert.isNull(orgId),SysRole::getOrgId,orgId);
        int allCount = Math.toIntExact(roleMapper.selectCount(wrapper));
        IPage<SysRole> page = new Page<>(crt,size);
        page.setTotal(allCount);
        page.setRecords(roleMapper.selectList(wrapper));
        return page;
    }

    @Override
    public List<SysRoleMenu> findRoleMenuList(Integer roleId) {
        return roleMapper.roleMenuList(roleId);
    }

    @Override
    public void addRoleMenu(Integer roleId, List<Integer> menuIds) {
        roleMapper.delRoleMenuByRoleId(roleId);
        if(!menuIds.isEmpty()){
            for (Integer menuId : menuIds) {
                roleMapper.insertUserRole(roleId,menuId);
            }
        }
    }
}
